package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.http.res.ResSpreadEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class SpreadAdp extends RecyclerView.Adapter<SpreadAdp.SpreadHolder>{

    private List<ResSpreadEntity.DataBean> mData;

    private Context mContext;

    private LayoutInflater inflater;

    public SpreadAdp(Context mContext){
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
    }

    public void setData(List<ResSpreadEntity.DataBean> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<ResSpreadEntity.DataBean> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public SpreadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_spread,parent,false);
        return new SpreadHolder(view);
    }

    @Override
    public void onBindViewHolder(SpreadHolder holder, int position) {
        final ResSpreadEntity.DataBean entity = mData.get(position);
        holder.update(entity);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class SpreadHolder extends RecyclerView.ViewHolder{

        private TextView mProductNameTv;

        private TextView mBuyerTv;

        private TextView mIncomeTv;

        private TextView mTimeTv;

        public SpreadHolder(View itemView) {
            super(itemView);
            mProductNameTv = itemView.findViewById(R.id.item_spread_product_name_tv);
            mBuyerTv = itemView.findViewById(R.id.item_spread_buyer_tv);
            mIncomeTv = itemView.findViewById(R.id.item_spread_income_tv);
            mTimeTv = itemView.findViewById(R.id.item_spread_time_tv);
        }

        public void update(ResSpreadEntity.DataBean entity){
            Log.d("dwqdwdqd", ":"+entity.getCreate_date()+":"+entity.getUsername()+":"+entity.getTotal_fee()+":"+entity.getProduct_name());
            if(!TextUtils.isEmpty(entity.getCreate_date())) {
                mTimeTv.setText(TimeUtils.timeFomartOnlyDate(Long.valueOf(entity.getCreate_date())));
            }
            mBuyerTv.setText(entity.getNext_username());
            mIncomeTv.setText(String.valueOf(entity.getTotal_fee()));
            mProductNameTv.setText(entity.getProduct_name());
        }
    }
}
